<template>
  <el-container class="layout-container">
    <!-- 顶部导航 -->
    <el-header class="header">
      <div class="logo">
        <el-icon class="logo-icon">
          <School />
        </el-icon>
        <span class="logo-text">宿舍管理系统</span>
      </div>
      <div class="header-right">
        <el-badge :value="3" class="notice-badge">
          <el-icon>
            <Bell />
          </el-icon>
        </el-badge>
        <div class="weather-info">
          <el-icon>
            <Sunny />
          </el-icon>
          <span>23°C</span>
        </div>
        <el-dropdown>
          <span class="user-info">
            <el-avatar :size="32" :src="userAvatar" />
            <span>{{ userName }}</span>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item class="dropdown-item">
                <el-icon>
                  <User />
                </el-icon>个人信息
              </el-dropdown-item>
              <el-dropdown-item class="dropdown-item">
                <el-icon>
                  <Setting />
                </el-icon>系统设置
              </el-dropdown-item>
              <el-dropdown-item divided @click="handleLogout" class="dropdown-item">
                <el-icon>
                  <SwitchButton />
                </el-icon>退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <el-container class="main-container">
      <!-- 侧边菜单 -->
      <el-aside :width="isCollapse ? '64px' : '220px'" class="aside-menu">
        <div class="menu-toggle" @click="toggleMenu">
          <el-icon>
            <Fold v-if="!isCollapse" />
            <Expand v-else />
          </el-icon>
        </div>
        <el-menu :default-active="activeMenu" class="sidebar-menu" :collapse="isCollapse" router>
          <el-menu-item index="/college">
            <el-icon>
              <Monitor />
            </el-icon>
            <template #title>学院列表</template>
          </el-menu-item>

          <el-sub-menu index="dorm">
            <template #title>
              <el-icon>
                <House />
              </el-icon>
              <span>宿舍管理</span>
            </template>
            <el-menu-item index="/dorm/list">宿舍列表</el-menu-item>
          </el-sub-menu>

          <el-sub-menu index="student">
            <template #title>
              <el-icon>
                <User />
              </el-icon>
              <span>学生管理</span>
            </template>
            <el-menu-item index="/student/list">学生列表</el-menu-item>
          </el-sub-menu>

          <el-menu-item index="/repair">
            <el-icon>
              <Tools />
            </el-icon>
            <template #title>维修申请</template>
          </el-menu-item>
          <el-menu-item index="/notice">
            <el-icon>
              <Bell />
            </el-icon>
            <template #title>通知公告</template>
          </el-menu-item>


        </el-menu>
      </el-aside>

      <!-- 主要内容区 -->
      <el-main class="main-content">
        <div class="page-header">
          <h2>{{ currentPageTitle }}</h2>
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ currentPageTitle }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <router-view v-slot="{ Component }">
          <transition name="fade-transform" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { removeToken } from '@/util/auth'
import {
  Monitor,
  House,
  User,
  Tools,
  Bell,
  School,
  Setting,
  SwitchButton,
  Fold,
  Expand,
  Sunny
} from '@element-plus/icons-vue'

const router = useRouter()

// 用户信息
const userName = ref('管理员')
const userAvatar = ref('https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png')
const isCollapse = ref(false)
const activeMenu = ref(router.currentRoute.value.path)
const currentPageTitle = ref('学院列表')

// 退出登录
const handleLogout = () => {
  // 清除 token
  removeToken()
  router.push('/login')
}

// 切换菜单展开/收起
const toggleMenu = () => {
  isCollapse.value = !isCollapse.value
}

// 监听路由变化更新页面标题
watch(() => router.currentRoute.value.path, (newPath) => {
  const routeMap = {
    '/college': '学院列表',
    '/dorm/list': '宿舍列表',
    '/dorm/assign': '宿舍分配',
    '/student/list': '学生列表',
    '/student/check': '学生考勤',
    '/repair': '维修申请',
    '/notice': '通知公告',
    '/visitor': '访客登记'
  }
  currentPageTitle.value = routeMap[newPath] || '学院列表'
})
</script>

<style scoped>
.layout-container {
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.layout-container::before {
  content: '';
  position: absolute;
  width: 200%;
  height: 200%;
  background: rgba(255, 255, 255, 0.1);
  transform: rotate(-45deg);
  animation: wave 8s infinite linear;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  z-index: 10;
  position: relative;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 100%;
}

.logo-icon {
  font-size: 28px;
  color: #667eea;
  animation: float 3s ease-in-out infinite;
}

.logo-text {
  font-size: 22px;
  font-weight: 600;
  background: linear-gradient(90deg, #667eea, #764ba2);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 24px;
  height: 100%;
}

.notice-badge {
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 20px;
  display: flex;
  align-items: center;
}

.weather-info {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #666;
  font-size: 14px;
  padding: 6px 12px;
  background: rgba(102, 126, 234, 0.1);
  border-radius: 20px;
}

.weather-info .el-icon {
  font-size: 18px;
  color: #667eea;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 12px;
  border-radius: 20px;
  transition: all 0.3s ease;
  background: rgba(102, 126, 234, 0.05);
}

.user-info:hover {
  background: rgba(102, 126, 234, 0.1);
  transform: translateY(-1px);
}

:deep(.el-badge__content) {
  background-color: #667eea;
}

:deep(.el-avatar) {
  border: 2px solid rgba(102, 126, 234, 0.2);
}

.aside-menu {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-right: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  margin: 16px 0 16px 16px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
}

.main-content {
  margin: 16px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.page-header {
  padding: 20px;
  border-bottom: 1px solid rgba(102, 126, 234, 0.2);
}

/* 动画效果 */
@keyframes wave {
  0% {
    transform: rotate(-45deg) translate(-50%, -50%);
  }

  100% {
    transform: rotate(-45deg) translate(50%, 50%);
  }
}

@keyframes float {

  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-5px);
  }
}

/* Element Plus 样式重写 */
:deep(.el-menu) {
  border-right: none;
  background: transparent;
}

:deep(.el-menu-item.is-active) {
  background: linear-gradient(90deg, rgba(102, 126, 234, 0.2), transparent);
  color: #667eea;
}

:deep(.el-menu-item):hover {
  background: rgba(102, 126, 234, 0.1);
}

:deep(.el-dropdown-menu) {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
}

/* 过渡动画 */
.fade-transform-enter-active,
.fade-transform-leave-active {
  transition: all 0.5s;
}

.fade-transform-enter-from {
  opacity: 0;
  transform: translateX(-30px);
}

.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(30px);
}
</style>
